/*
 * Bomberman.java
 *
 * Created on March 20, 2008, 8:42 PM
 */
package no.eirikb.bomberman.applet;

import no.eirikb.bomberman.applet.game.GamePanel;
import no.eirikb.bomberman.applet.menu.MenuPanel;
import no.eirikb.bomberman.applet.chat.ChatContainerPanel;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import no.eirikb.bomberman.client.BombermanClient;
import no.eirikb.bomberman.server.Server;
import no.eirikb.bomberman.shared.Game;
import no.eirikb.bomberman.shared.User;
import no.eirikb.bomberman.shared.img.ImageDB;

/**
 *
 * @author  eirikb
 */
public class Bomberman extends javax.swing.JApplet {

    private ImageDB idb;
    private Server server;
    private User user;
    private BombermanClient client; // Used for appending functionality to client
    private Double hash;
    private final int W = 640;
    private final int H = 480;
    private final String HOST = "rmi://localhost/BOMBERMAN";
    private GameWaitPanel gameWaitPanel;

    /** Initializes the applet Bomberman */
    @Override
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
            setSize(W, H + 100);
            client = new BombermanClient(this);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void stop() {
        try {
            if (user != null) {
                server.disconnect(hash, user.getNick());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.stop();
    }

    public void show(String c) {
        // mainPanel vould me null when testing!
        if (mainPanel != null) {
            CardLayout layout = (CardLayout) mainPanel.getLayout();
            layout.show(mainPanel, c);
        }
    }

    /**
     * When LoginPanel succeeds this method is called
     * you might notice this is horrible programming, and that is true.
     * But remeber that beer is nice
     * @param idb ImageDB receieved from server
     * @param menuPanel MenuPanel created by LoginPanel
     */
    public void loginOK() {
        ChatContainerPanel chatPanel = new ChatContainerPanel(this);
        client.setChat(chatPanel);
        client.setGameWaitPanel(gameWaitPanel);
        add(chatPanel, BorderLayout.SOUTH);
        MenuPanel menuPanel = new MenuPanel(this);
        client.setMenuPanel(menuPanel);
        mainPanel.add(menuPanel, "menu");
        show("menu");
    }

    public void showGameWaitPanel(Game game) {
        show("gamewait");
        gameWaitPanel.reset(game);
    }

    public String getHOST() {
        return HOST;
    }

    public BombermanClient getClient() {
        return client;
    }

    public void setClient(BombermanClient client) {
        this.client = client;
    }

    public Double getHash() {
        return hash;
    }

    public void setHash(Double hash) {
        this.hash = hash;
    }

    public ImageDB getIdb() {
        return idb;
    }

    public void setIdb(ImageDB idb) {
        this.idb = idb;
    }

    public Server getServer() {
        return server;
    }

    public void setServer(Server server) {
        this.server = server;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();

        mainPanel.setMinimumSize(new java.awt.Dimension(640, 480));
        mainPanel.setLayout(new java.awt.CardLayout());
        mainPanel.add(new LoginPanel(this), "login");
        mainPanel.add(gameWaitPanel = new GameWaitPanel(this), "gamewait");
        mainPanel.add(new GamePanel(this), "game");
        getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables
}
